<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="order_id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="order_id"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderSn">
      <el-input v-model="dataForm.orderSn" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="商品分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="商品分类id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="productName"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="productPic">
      <el-input v-model="dataForm.productPic" placeholder="product_pic"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品购买的数量" prop="productAmount">
      <el-input v-model="dataForm.productAmount" placeholder="商品购买的数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          orderSn: '',
          categoryId: '',
          productId: '',
          productName: '',
          productPic: '',
          productPrice: '',
          productAmount: ''
        },
        dataRule: {
          orderId: [
            { required: false, message: 'order_id不能为空', trigger: 'blur' }
          ],
          orderSn: [
            { required: false, message: '订单号不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: false, message: '商品分类id不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: false, message: '商品id不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: false, message: 'product_name不能为空', trigger: 'blur' }
          ],
          productPic: [
            { required: false, message: 'product_pic不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: false, message: '商品价格不能为空', trigger: 'blur' }
          ],
          productAmount: [
            { required: false, message: '商品购买的数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornApi(`/orderitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.orderItem.orderId
                this.dataForm.orderSn = data.orderItem.orderSn
                this.dataForm.categoryId = data.orderItem.categoryId
                this.dataForm.productId = data.orderItem.productId
                this.dataForm.productName = data.orderItem.productName
                this.dataForm.productPic = data.orderItem.productPic
                this.dataForm.productPrice = data.orderItem.productPrice
                this.dataForm.productAmount = data.orderItem.productAmount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornApi(`/orderitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'orderSn': this.dataForm.orderSn,
                'categoryId': this.dataForm.categoryId,
                'productId': this.dataForm.productId,
                'productName': this.dataForm.productName,
                'productPic': this.dataForm.productPic,
                'productPrice': this.dataForm.productPrice,
                'productAmount': this.dataForm.productAmount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
